package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.SkinModel;
import com.zw.yzk.kabu.view.activity.SkinPackageActivity;

import java.util.List;

/**
 * Created by skydar on 2017/4/13.
 */

public class SkinListAdapter extends DelegateAdapter.Adapter<SkinListAdapter.Holder> {

    private Context context;
    private LayoutHelper layoutHelper;
    private List<SkinModel> dataList;

    public SkinListAdapter(Context context, List<SkinModel> models) {
        GridLayoutHelper helper = new GridLayoutHelper(6);
        helper.setBgColor(Color.WHITE);
        int margin = (int) (6 * context.getResources().getDisplayMetrics().density);
        helper.setMarginBottom(margin);
        helper.setPaddingLeft(margin);
        helper.setPaddingRight(margin);
        helper.setAutoExpand(false);
        helper.setSpanCount(3);
        this.layoutHelper = helper;
        this.context = context;
        this.dataList = models;
    }

    @Override
    public SkinListAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_skin_category_content_list, parent, false);
        return new SkinListAdapter.Holder(view);
    }

    @Override
    public void onBindViewHolder(final SkinListAdapter.Holder holder, int position) {
        final SkinModel model = dataList.get(position);

        holder.name.setText(model.getName());

        holder.head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent intent = new Intent(context, SkinPackageActivity.class);
            intent.putExtra(ActivityConstant.SKIN_PACKAGE_ID, model.getId());
            intent.putExtra(ActivityConstant.SKIN_PACKAGE_NAME, model.getName());
            context.startActivity(intent);
            }
        });

        Glide.with(context)
                .load(model.getCoverPath())
                .error(R.drawable.im_default)
                .placeholder(R.drawable.im_default)
                .centerCrop()
                .into(holder.head);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.head)
        ImageView head;
        @Bind(R.id.name)
        TextView name;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}